package com.hhp.hhptt.config;

import com.hhp.hhptt.common.interceptor.JwtInterceptor;
import com.hhp.hhptt.utils.commonUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class InteceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有请求判断token是否合法是否需要登录
        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**").excludePathPatterns("/**/login","/**/register","**/import","**/export");
    }

    @Bean
    public JwtInterceptor jwtInterceptor(){
        return new JwtInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String fileAbsolutePath="";
        String SwiperfileAbsolutePath="";
        String teacherXinGong="";
        String MapXinGong="";
        String imagesXIGongShang="";
        String iconString="";
        String pictureString="";
        String videoString="";
        try {
            fileAbsolutePath=commonUtils.getFileAbsolutePathDeep("digtalcampus","navigator","menu","");
            SwiperfileAbsolutePath=commonUtils.getFileAbsolutePathDeep("digtalcampus","swiper","swiperImage","");
            teacherXinGong=commonUtils.getFileAbsolutePathDeep("digtalcampus","teachers","xinxigongchengxueyuan","");
            MapXinGong=commonUtils.getFileAbsolutePathDeep("digtalcampus","img","school","");
            imagesXIGongShang=commonUtils.getFileAbsolutePathDeep("digtalcampus","img","images","");
            iconString=commonUtils.getFileAbsolutePathDeep("digtalcampus","img","icon","");
            pictureString=commonUtils.getFileAbsolutePathDeep("digtalcampus","picture","images","");
            videoString=commonUtils.getFileAbsolutePathDeep("digtalcampus","img","video","");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        //导航栏
        String localFilePath="file:\\\\"+fileAbsolutePath+"\\\\";
        String replace = localFilePath.replace("\\\\", "/");
        String replace1 = replace.replace("//", "/");
        //轮播图
        String localFilePath1="file:\\\\"+SwiperfileAbsolutePath+"\\\\";
        String replaceSwiper = localFilePath1.replace("\\\\", "/");
        String replaceSwiper1 = replaceSwiper.replace("//", "/");

        //教师信工学院
        String localFilePath2="file:\\\\"+teacherXinGong+"\\\\";
        String replaceTeacher = localFilePath2.replace("\\\\", "/");
        String replaceTeacher2 = replaceTeacher.replace("//", "/");

        //地图导航
        String localFilePath3="file:\\\\"+MapXinGong+"\\\\";
        String replaceMap = localFilePath3.replace("\\\\", "/");
        String replaceMap3 = replaceMap.replace("//", "/");

        //影像西工商
        String localFilePath4="file:\\\\"+imagesXIGongShang+"\\\\";
        String replaceimages = localFilePath4.replace("\\\\", "/");
        String replaceimages4 = replaceimages.replace("//", "/");


        //图标
        String localFilePath5="file:\\\\"+iconString+"\\\\";
        String replaceimages5 = localFilePath5.replace("\\\\", "/");
        String iconMapping = replaceimages5.replace("//", "/");

        //图片
        String localFilePath6="file:\\\\"+pictureString+"\\\\";
        String replaceimages6 = localFilePath6.replace("\\\\", "/");
        String pictureMapping = replaceimages6.replace("//", "/");

        //视频相关
        String localFilePath7="file:\\\\"+videoString+"\\\\";
        String replaceimages7 = localFilePath7.replace("\\\\", "/");
        String videoMapping = replaceimages7.replace("//", "/");


        registry.addResourceHandler("/images/**")
                .addResourceLocations(replace1,replaceSwiper1,replaceTeacher2,replaceMap3,pictureMapping);

        registry.addResourceHandler("/images/images/**")
                .addResourceLocations(replaceimages4);

        registry.addResourceHandler("/icon/**")
                .addResourceLocations(iconMapping);
        registry.addResourceHandler("/find/**")
                .addResourceLocations(videoMapping);
    }
}
